﻿using Lamex.Common;
using Lamex.SystemManager.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Lamex.Base.Framework.BaseDal;
using Lamex.Base;
using Lamex.SystemManager.Interfaces;


namespace Lamex.SystemManager.DalServer
{
    public class TUser : BaseDalSQL<TUserInfo>, IUser
    {
        //public DataTable GetUsers()
        //{
        //    string sql = "select * from [tb_MyUser]";

        //    return DataProvider.Instance.GetTable(sql, "tb_MyUser");
        //}

        ///// <summary>
        ///// 检查用户是否存在
        ///// </summary>
        ///// <param name="userid">用户编号</param>
        ///// <returns></returns>
        //public bool ExistsUser(string account)
        //{
        //    string sql = "select count(*) from [tb_MyUser] where [Account]=@Account";
        //    SqlCommandBase cmd = SqlBuilder.BuildSqlCommandBase(sql);
        //    cmd.AddParam("@Account", SqlDbType.VarChar, account);
        //    object o = DataProvider.Instance.ExecuteScalar(cmd.SqlCommand);
        //    return int.Parse(o.ToString()) > 0;
        //}

        /// <summary>
        /// 登出
        /// </summary>
        /// <param name="account">登录帐号</param>
        public void Logout(string account)
        {
            //SqlCommandBase cmd = SqlBuilder.BuildSqlProcedure("sp_Logout");
            //cmd.AddParam("@Account", SqlDbType.VarChar, account);
            //DataProvider.Instance.ExecuteNoQuery(cmd.SqlCommand);

        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userID">登录帐号</param>
        /// <returns></returns>
        public DataSet Login(LoginUser loginUser, char LoginUserType)
        {
            //SqlCommandBase cmd = SqlBuilder.BuildSqlProcedure("sp_Login");
            //cmd.AddParam("@Account", SqlDbType.VarChar, loginUser.Account);
            //cmd.AddParam("@Password", SqlDbType.VarChar, loginUser.Password);
            //cmd.AddParam("@DataSetID", SqlDbType.VarChar, loginUser.DataSetID);
            //cmd.AddParam("@LoginUserType", SqlDbType.Char, LoginUserType);
            List<string> lst = new List<string>();
            lst.Add(loginUser.Account);
            lst.Add(loginUser.Password);
            lst.Add(loginUser.DataSetID);
            lst.Add(LoginUserType.ToString());

            DataSet ds = GetProDataSet("sp_Login", lst);
            
            return ds;
        }

        ///// <summary>
        ///// 跟据Novell网帐号获取系统帐号
        ///// </summary>
        ///// <param name="novellAccount">Novell网帐号</param>
        ///// <returns></returns>
        //public DataTable GetUserByNovellID(string novellAccount)
        //{
        //    string sql = "select * from [tb_MyUser] where [NovellAccount]=@novellAccount";
        //    SqlCommandBase cmd = SqlBuilder.BuildSqlCommandBase(sql);
        //    cmd.AddParam("@novellAccount", SqlDbType.VarChar, novellAccount);
        //    DataTable dt = DataProvider.Instance.GetTable(cmd.SqlCommand, TUser.__TableName);
        //    return dt;
        //}

        /// <summary>
        /// 获取当前用户的系统权限
        /// </summary>
        /// <param name="user">当前用户</param>
        /// <returns></returns>
        public DataTable GetUserAuthorities(Loginer user)
        {
            //SqlProcedure sp = SqlBuilder.BuildSqlProcedure("p_MyGetAuthorities");
            //sp.AddParam("@UserOrGroup", SqlDbType.VarChar, user.Account);
            //sp.AddParam("@Type", SqlDbType.Int, 1);
            List<string> lst = new List<string>();
            lst.Add(user.Account);
            lst.Add("1");
            DataTable dt = GetProDataTable("p_MyGetAuthorities", lst);
            return dt;
        }

        public List<TUserInfo> GetUserList(int RoleId)
        {
            string sql = "SELECT * FROM [dbo].[Sys_MyUser] INNER JOIN [dbo].[Sys_UserRole]  On [dbo].[Sys_MyUser].IsId=[dbo].[Sys_UserRole].UserID WHERE RoleID='" + RoleId + "'";
            DataTable dt = GetDataTableBySql(sql);
            List<TUserInfo> lst= ConvertHelper<TUserInfo>.DataTableToList(dt);
            return lst;
        }
    }
}
